package cn.yyh.servicePay.utils;

import cn.yyh.servicePay.domain.AdoptOrder;
import cn.yyh.servicePay.domain.ProductOrder;
import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;


/**
 * 实体转化工具类ConvertUtils
 *
 * @version: 1.0
 * @return: null
 * @author: 杨永航
 * @date: 2021/7/11
 */
public class ConvertUtils {

    public static JSONObject adoptOrder(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONObject("adopt");
    }
    public static JSONObject productOrder(String info){
        return JSONObject.parseObject(info).getJSONObject("data").getJSONObject("product");
    }



    public static AdoptOrder toAdoptOrder(JSONObject info) {
        AdoptOrder adopt = new AdoptOrder();
        if ((info != null) && (!info.isEmpty())) {
            if (null != info.get("id")) {
                adopt.setId(Long.valueOf(String.valueOf(info.get("id"))));

            }
            if (null != info.get("digest")) {
                adopt.setDigest(String.valueOf(info.get("digest")));

            }
            if (null != info.get("price")) {
                adopt.setPrice(new BigDecimal(String.valueOf(info.get("price"))));

            }
            if (null != info.get("orderSn")) {
                adopt.setOrderSn(String.valueOf(info.get("orderSn")));

            }
            if (null != info.get("paySn")) {
                adopt.setPaySn(String.valueOf(info.get("paySn")));
            }
            if (null != info.get("state")) {
                adopt.setState(Integer.valueOf(String.valueOf(info.get("state"))));
            }
            if (null != info.get("pet_id")) {
                adopt.setPet_id(Long.valueOf(String.valueOf(info.get("pet_id"))));
            }
            if (null != info.get("user_id")) {
                adopt.setUser_id(Long.valueOf(String.valueOf(info.get("user_id"))));
            }
            if (null != info.get("shop_id")) {
                adopt.setShop_id(Long.valueOf(String.valueOf(info.get("shop_id"))));
            }
            if (null != info.get("lastPayTime")) {
                try {
                    adopt.setLastPayTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("lastPayTime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (null != info.get("lastConfirmTime")) {
                try {
                    adopt.setLastConfirmTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("lastConfirmTime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            return adopt;
        }
        return null;
    }

    public static ProductOrder toProductOrder(JSONObject info) {
        ProductOrder order = new ProductOrder();
        if ((info != null) && (!info.isEmpty())) {
            if (null != info.get("id")) {
                order.setId(Long.valueOf(String.valueOf(info.get("id"))));

            }
            if (null != info.get("digest")) {
                order.setDigest(String.valueOf(info.get("digest")));

            }
            if (null != info.get("price")) {
                order.setPrice(new BigDecimal(String.valueOf(info.get("price"))));

            }
            if (null != info.get("orderSn")) {
                order.setOrderSn(String.valueOf(info.get("orderSn")));

            }
            if (null != info.get("paySn")) {
                order.setPaySn(String.valueOf(info.get("paySn")));
            }
            if (null != info.get("state")) {
                order.setState(Integer.valueOf(String.valueOf(info.get("state"))));
            }
            if (null != info.get("product_id")) {
                order.setProduct_id(Long.valueOf(String.valueOf(info.get("product_id"))));
            }
            if (null != info.get("user_id")) {
                order.setUser_id(Long.valueOf(String.valueOf(info.get("user_id"))));
            }
            if (null != info.get("shop_id")) {
                order.setShop_id(Long.valueOf(String.valueOf(info.get("shop_id"))));
            }
            if (null != info.get("lastPayTime")) {
                try {
                    order.setLastPayTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("lastPayTime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (null != info.get("lastConfirmTime")) {
                try {
                    order.setLastConfirmTime(DateFormat.getDateTimeInstance().parse(String.valueOf(info.get("lastConfirmTime"))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            return order;
        }
        return null;
    }

}
